/*
	new Ajax("GET" || "POST", "url", postVars)
 */
var Ajax = function(method, url, postVars){
	this.method = method;
	this.url = url;
	this.postVars = postVars;
};

Ajax.prototype = {
	request: function(callback){
		var xhr = this.createXMLObject();
		if(this.method.toUpperCase() == "GET"){
			this.url += "?" + this.JSONStringify(this.postVars) + Math.random();
			this.postVars = null;
		}
		xhr.open(this.method, this.url, true);
		//xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
		xhr.send(this.postVars);
		xhr.onreadystatechange = function(){
			if(xhr.readyState !== 4)return;
			//console.log(xhr);
			(xhr.status === 200) ? 
				callback(xhr.responseText, xhr.responseXML) : //callback(text,xml);
				callback(xhr.status, xhr.status);
		}
	},
	createXMLObject: function(){
		var methods = [
			function(){return new XMLHttpRequest()},
			function(){return new ActiveXObject("Microsoft.XMLHTTP")},
			function(){return new ActiveXObject("MSXML2.XMLHTTP")}
		],
		i = 0,
		len = methods.length;
		for(; i < len; i++){
			try{
				methods[i];
			}catch(e){
				continue;
			}
			this.createXMLObject = methods[i];
			return methods[i]();
		}
		return new Error("ajax create failure");
	},
	JSONStringify: function(obj){
		return JSON.stringify(obj).replace(/"|{|}/g, "")
			.replace(/:/g, "=")
			.replace(/,/g, "&");
	}
}